package handler

import (
	v1 "gin-vben-admin/api/v1"
	"gin-vben-admin/api/v1/errcode"
	"gin-vben-admin/internal/constant"
	"gin-vben-admin/internal/service"
	"github.com/gin-gonic/gin"
	"strconv"
)

type SysUserHandler struct {
	*Handler
	sysUserService service.SysUserService
}

func NewSysUserHandler(handler *Handler, userService service.SysUserService) *SysUserHandler {
	return &SysUserHandler{
		Handler:        handler,
		sysUserService: userService,
	}
}

func (h *SysUserHandler) GetPageList(ctx *gin.Context) {
	var req v1.SysUserListReq
	if err := ctx.ShouldBind(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	total, data, err := h.sysUserService.GetPageList(ctx, &req)
	v1.PageData(ctx, total, data, err)
}

func (h *SysUserHandler) Create(ctx *gin.Context) {
	var req = v1.SysUserCreateReq{AuthId: UserId(ctx)}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysUserService.Create(ctx, &req), nil)
}

func (h *SysUserHandler) Update(ctx *gin.Context) {
	var req = v1.SysUserUpdateReq{AuthId: UserId(ctx)}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysUserService.Update(ctx, &req), nil)
}

func (h *SysUserHandler) Delete(ctx *gin.Context) {
	_id := ctx.Param("uid")
	id, err := strconv.ParseInt(_id, 10, 64)
	if err != nil {
		v1.Error(ctx, errcode.ErrBadRequest)
		return
	}
	v1.Response(ctx, h.sysUserService.Delete(ctx, id), nil)
}

// Detail 获取用户详情
// @Summary 获取用户详情
// @Tags 系统管理/用户管理
// @param uid path int true "用户id"
// @Produce json
// @Response 200 {object} response.Response{data=models.SysUser}
// @Router /api/v1/sys/user/{uid} [get]
// @Security Bearer
func (h *SysUserHandler) Detail(ctx *gin.Context) {
	_id := ctx.Param("uid")
	id, err := strconv.ParseInt(_id, 10, 64)
	if err != nil || id == 0 {
		v1.Error(ctx, errcode.ErrBadRequest)
		return
	}
	data, err := h.sysUserService.Detail(ctx, id)
	v1.Response(ctx, err, data)
}

// Enable 启用用户
// @Summary 启用用户
// @Tags 系统管理/用户管理
// @param uid path int true "用户id"
// @Produce json
// @Response 200 {object} response.Response
// @Router /api/v1/sys/user/{uid}/enable [put]
// @Security Bearer
func (h *SysUserHandler) Enable(ctx *gin.Context) {
	_id := ctx.Param("uid")
	id, err := strconv.ParseInt(_id, 10, 64)
	if err != nil || id == 0 {
		v1.Error(ctx, errcode.ErrBadRequest)
		return
	}
	v1.Response(ctx, h.sysUserService.EditStatus(ctx, id, constant.StatusEnable), nil)
}

// Disable 禁用用户
// @Summary 禁用用户
// @Tags 系统管理/用户管理
// @param uid path int true "用户id"
// @Produce json
// @Response 200 {object} response.Response
// @Router /api/v1/sys/user/{uid}/disable [put]
// @Security Bearer
func (h *SysUserHandler) Disable(ctx *gin.Context) {
	_id := ctx.Param("uid")
	id, err := strconv.ParseInt(_id, 10, 64)
	if err != nil || id == 0 {
		v1.Error(ctx, errcode.ErrBadRequest)
		return
	}
	v1.Response(ctx, h.sysUserService.EditStatus(ctx, id, constant.StatusDisable), nil)
}

// ResetPassword 修改密码
// @Summary 修改密码
// @Tags 系统管理/用户管理
// @param json body sys.UserResetPasswordReq true "密码"
// @Produce json
// @Response 200 {object} response.Response
// @Router /api/v1/sys/reset_password [put]
// @Security Bearer
func (h *SysUserHandler) ResetPassword(ctx *gin.Context) {
	var req v1.SysUserResetPwdReq
	if err := ctx.ShouldBind(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysUserService.ResetPassword(ctx, UserId(ctx), &req), nil)
}
